package com.example.ywj.myapplication.login;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.ywj.myapplication.R;

import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import javax.net.ssl.HttpsURLConnection;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_user,et_pwd;
    private Button btn_login;
    private int status;
    private static final String TAG = "LoginActivity";
    String message;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Log.i(TAG, "onCreate: ");
        initView();
    }
    public void initView(){
        et_user = findViewById(R.id.editText_user);
        et_pwd = findViewById(R.id.editText_pwd);
        btn_login = findViewById(R.id.button_login);
        btn_login.setOnClickListener(this);
    }
    public void func(){
        new Thread(){
            @Override
            public void run() {
                String statu = getData();
                Log.i(TAG,statu+"");
                // handler发送消息
                //Message msg = Message.obtain();
                //  发送给主线程的标记
                ///msg.what = statu;
                // 发送给主线程的对象
                // 发送消息
                //handler.sendMessage(msg);
            }
        }.start();
    }
    Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
//            switch (msg.what){
//                case 0:
//                    //Toast.makeText(LoginActivity.this,msg.what,Toast.LENGTH_LONG).show();
//                    Intent intent = new Intent(LoginActivity.this,SuccessActivity.class);
//                    startActivity(intent);
//                    break;
//            }
        }
    };
    public String getData(){
        try{
            URL url =new URL("http://10.15.180.121:3002/checkLogin");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            // 4.设置超时时间
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(10000);
            String data = "username=" + URLEncoder.encode(et_pwd.getText().toString().trim())
                    + "&password=" + URLEncoder.encode(et_user.getText().toString().trim());
            Log.i(TAG,data);
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", data.length() + "");
            conn.setDoOutput(true);     // post请求必须要设置（设置是否向httpURLConnection输出）
            OutputStream os = conn.getOutputStream();
            os.write(data.getBytes());
            int responseCode =  conn.getResponseCode();
            // 返回200时，成功获取数据
            if(responseCode == 200){
                InputStream is = conn.getInputStream();
                byte[] b = new byte[1024*512];
                int len = 0;
                //建立缓存流，保存所读取的字节数组
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                while ((len = is.read(b)) > -1){
                    baos.write(b,0,len);
                }
                String msg = baos.toString();
                Log.i(TAG, "getData: "+msg);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return message;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_login:
                func();
                break;
        }
    }
}
